<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Axios</title>
  </head>
  <body>
    <script>
      function Axios(config) {
        this.defaultConfig = config
        this.intercepters = {
          requestIntercepter: function () {},
          responseIntercepter: function () {}
        }
      }

      Axios.prototype.request = function (config) {
        console.log('发送 AJAX 请求 method=', config.method,this)
      }
      Axios.prototype.get = function (config) {
        console.log('发送 get AJAX 请求 method=', config.method)
        this.request(config)
      }
      Axios.prototype.post = function (config) {
        console.log('发送 post AJAX 请求 method=', config.method)
        this.request(config)
      }

     

      function createInstance(config ) {
        let context = new Axios(config)
        console.log('context 1', context)
        
        let instance = Axios.prototype.request.bind(context)
        console.log('instance', instance)
        console.log('context 2', context)
        Object.keys(Axios.prototype).forEach((key) => {
          instance[key] = Axios.prototype[key].bind(context)
        })
        console.log('context 3', context)
        console.log('instance 2', instance)

        Object.keys(context).forEach((key) => {
          instance[key] = context[key]
        })
        console.log('context 4', context)
        console.log(instance)
        return instance;
      }

      let axios =createInstance({"method":"post"})
      console.log('axios',axios({"method":"post"}))
    </script>
  </body>
</html>
